package com.mbc.web.common.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.common.entities.Currency;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateRpt;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.Modal;
import com.mbc.web.common.client.ui.SearchCurrencyRate;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.MTextBox;
import com.mbc.web.common.shared.HttpStatus;

public class FrmCurrencyRateRpt extends Composite {

	@UiField
	MButton btnPreview;
	@UiField
	MButton btnClear;
	
	@UiField
	MButton btnfromCurRateID;
	@UiField
	MButton btntoCurRateID;
	@UiField
	MButton btnfromSCurID;
	@UiField
	MButton btntoSCurID;
	@UiField
	MButton btnfromCCurID;
	@UiField
	MButton btntoCCurID;

	@UiField
	TextBox txtfromCurRateID;
	@UiField
	MTextBox txttoCurRateID;
	@UiField
	MTextBox txtfromSCurID;
	@UiField
	MTextBox txttoSCurID;
	@UiField
	MTextBox txtfromCCurID;
	@UiField
	MTextBox txttoCCurID;

	private CurrencyRate currencyrate;
	private Currency sourceCur;
	private Currency convertCur;

	byte action_type;

	private static FrmCurrencyRateRptUiBinder uiBinder = GWT
			.create(FrmCurrencyRateRptUiBinder.class);

	interface FrmCurrencyRateRptUiBinder extends
			UiBinder<Widget, FrmCurrencyRateRpt> {
	}

	public FrmCurrencyRateRpt() {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	public FrmCurrencyRateRpt(CurrencyRateRpt crate) {
		initWidget(uiBinder.createAndBindUi(this));
		initialize();
	}

	private void initialize() {
		
		txtfromCCurID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoCCurID.getElement().setAttribute("style", "text-transform: uppercase;");
		txtfromCurRateID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoCurRateID.getElement().setAttribute("style", "text-transform: uppercase;");
		txtfromSCurID.getElement().setAttribute("style", "text-transform: uppercase;");
		txttoSCurID.getElement().setAttribute("style", "text-transform: uppercase;");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				txtfromCurRateID.setFocus(true);
			}
		});
		txttoCCurID.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					event.preventDefault();
					TextBox l_txt = (TextBox) event.getSource();
					if (l_txt == txttoCCurID)
						btnPreview.setFocus(true);
				}
			}
		});
	}

	private CurrencyRateRpt bindCRateRpt() {
		CurrencyRateRpt l_info = new CurrencyRateRpt();
		l_info.setFromCurrencyRateID(txtfromCurRateID.getText());
		l_info.setToCurrencyRateID(txttoCurRateID.getText());

		l_info.setFromSourceCurrencyID(txtfromSCurID.getText());
		l_info.setToSourceCurrencyID(txttoSCurID.getText());

		l_info.setFromConvertCurrencyID(txtfromCCurID.getText());
		l_info.setToConvertCurrencyID(txttoCCurID.getText());
		return l_info;
	}

	@UiHandler("btnPreview")
	void onBtnPreviewClick(ClickEvent event) {
		ViewUtils.closeMessageBox();
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.prepareCurrencyRateReport(bindCRateRpt(),
				CurrencyRateReportCallback());
	}

	private AsyncCallback<Integer> CurrencyRateReportCallback() {
		return new AsyncCallback<Integer>() {

			@Override
			public void onSuccess(Integer result) {
				ViewUtils.stopAjaxLoader();
				if (result == HttpStatus.OK) {
					ClientSingleton.viewReport("Currency Rate Report");
				} else if (result == HttpStatus.NOT_FOUND) {
					ViewUtils.showErrorMessage("No records.");
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	
	@UiHandler("btnClear")
	void onBtnClearClick(ClickEvent event) {
		txtfromCurRateID.setText("");
		txttoCurRateID.setText("");
		txtfromSCurID.setText("");
		txttoSCurID.setText("");
		txtfromCCurID.setText("");
		txttoCCurID.setText("");
		txtfromCurRateID.setFocus(true);
	}

	@UiHandler("btnfromCurRateID")
	void onBtnBwrfromCRateClick(ClickEvent event) {
		final SearchCurrencyRate l_dia = new SearchCurrencyRate("Currency Rate List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					currencyrate = l_dia.getSelectedEntity();
					txtfromCurRateID.setText(currencyrate.getCurrencyRateCode());

					txttoCurRateID.setFocus(true);
				} else
					txtfromCurRateID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoCurRateID")
	void onBtnBwrtoCRateClick(ClickEvent event) {
		final SearchCurrencyRate l_dia = new SearchCurrencyRate("Currency Rate List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					currencyrate = l_dia.getSelectedEntity();
					txttoCurRateID.setText(currencyrate.getCurrencyRateCode());
					txttoCurRateID.setFocus(true);
				} else
					txttoCurRateID.setFocus(true);
			}

		});
	}

	@UiHandler("btnfromSCurID")
	void onBtnBwrfromSCurClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					sourceCur = l_dia.getSelectedEntity();
					txtfromSCurID.setText(sourceCur.getCurrencyCode());

					txttoSCurID.setFocus(true);
				} else
					txtfromSCurID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoSCurID")
	void onBtnBwrtoSCurClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					sourceCur = l_dia.getSelectedEntity();
					txttoSCurID.setText(sourceCur.getCurrencyCode());
					txttoSCurID.setFocus(true);
				} else
					txttoSCurID.setFocus(true);
			}

		});
	}

	@UiHandler("btnfromCCurID")
	void onBtnBwrfromCCurClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					convertCur = l_dia.getSelectedEntity();
					txtfromCCurID.setText(convertCur.getCurrencyCode());

					txttoCCurID.setFocus(true);
				} else
					txtfromCCurID.setFocus(true);
			}
		});
	}

	@UiHandler("btntoCCurID")
	void onBtnBwrtoCCurClick(ClickEvent event) {
		final SearchCurrency l_dia = new SearchCurrency("Currency List");

		l_dia.showDialog(new CloseHandler<Modal>() {

			public void onClose(CloseEvent<Modal> event) {
				if (l_dia.getSelectedEntity() != null) {
					convertCur = l_dia.getSelectedEntity();
					txttoCCurID.setText(convertCur.getCurrencyCode());
					txttoCCurID.setFocus(true);
				} else
					txttoCCurID.setFocus(true);
			}

		});
	}
	
	@UiHandler("txtfromCurRateID")
	void onTxtFromCurrencyRateChange(ChangeEvent event) {
		if (!txtfromCurRateID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyRateByCode(txtfromCurRateID.getText(),
					getFromCurrencyRateCallback());
	}

	private AsyncCallback<CurrencyRate> getFromCurrencyRateCallback() {
		return new AsyncCallback<CurrencyRate>() {

			@Override
			public void onSuccess(CurrencyRate result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromCurRateID.setText(result.getCurrencyRateCode());
					txtfromCurRateID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyRateId()));
				} else {
					txtfromCurRateID.setText("");
					txtfromCurRateID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Rate Code does not exist.");
					txtfromCurRateID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoCurRateID")
	void onTxtToCurrencyRateChange(ChangeEvent event) {
		if (!txttoCurRateID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyRateByCode(txttoCurRateID.getText(),
					getToCurrencyRateCallback());
	}

	private AsyncCallback<CurrencyRate> getToCurrencyRateCallback() {
		return new AsyncCallback<CurrencyRate>() {

			@Override
			public void onSuccess(CurrencyRate result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoCurRateID.setText(result.getCurrencyRateCode());
					txttoCurRateID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyRateId()));
				} else {
					txttoCurRateID.setText("");
					txttoCurRateID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Rate Code does not exist.");
					txttoCurRateID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txtfromSCurID")
	void onTxtFromSCurrencyChange(ChangeEvent event) {
		if (!txtfromSCurID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txtfromSCurID.getText(),
					getFromSCurrencyCallback());
	}

	private AsyncCallback<Currency> getFromSCurrencyCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromSCurID.setText(result.getCurrencyCode());
					txtfromSCurID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyId()));
				} else {
					txtfromSCurID.setText("");
					txtfromSCurID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Code does not exist.");
					txtfromSCurID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoSCurID")
	void onTxtToSCurrencyChange(ChangeEvent event) {
		if (!txttoSCurID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txttoSCurID.getText(),
					getToSCurrencyCallback());
	}

	private AsyncCallback<Currency> getToSCurrencyCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoSCurID.setText(result.getCurrencyCode());
					txttoSCurID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyId()));
				} else {
					txttoSCurID.setText("");
					txttoSCurID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Code does not exist.");
					txttoSCurID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txtfromCCurID")
	void onTxtFromCCurrencyChange(ChangeEvent event) {
		if (!txtfromCCurID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txtfromCCurID.getText(),
					getFromCCurrencyCallback());
	}

	private AsyncCallback<Currency> getFromCCurrencyCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txtfromCCurID.setText(result.getCurrencyCode());
					txtfromCCurID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyId()));
				} else {
					txtfromCCurID.setText("");
					txtfromCCurID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Code does not exist.");
					txtfromCCurID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}
	@UiHandler("txttoCCurID")
	void onTxtToCCurrencyChange(ChangeEvent event) {
		if (!txttoCCurID.getText().equals(""))
			ViewUtils.startAjaxLoader(CommonMessage.loading_message);
			CommonControllerMgr.findCurrencyByCode(txttoCCurID.getText(),
					getToCCurrencyCallback());
	}

	private AsyncCallback<Currency> getToCCurrencyCallback() {
		return new AsyncCallback<Currency>() {

			@Override
			public void onSuccess(Currency result) {
				ViewUtils.stopAjaxLoader();
				if (result != null) {
					txttoCCurID.setText(result.getCurrencyCode());
					txttoCCurID.getElement().setAttribute("id",
							String.valueOf(result.getCurrencyId()));
				} else {
					txttoCCurID.setText("");
					txttoCCurID.getElement().setAttribute("id", "");
					ViewUtils.showErrorMessage("Currency Code does not exist.");
					txttoCCurID.setFocus(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
	}

	@UiHandler("txtfromCurRateID")
	void onTxtfromCRateKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromCRateClick(null);
		}
	}
	@UiHandler("txttoCurRateID")
	void onTxttoCRateKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrtoCRateClick(null);
		}
	}
	@UiHandler("txtfromSCurID")
	void onTxtfromSCurKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromSCurClick(null);
		}
	}
	@UiHandler("txttoSCurID")
	void onTxttoSCurKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrtoSCurClick(null);
		}
	}
	@UiHandler("txtfromCCurID")
	void onTxtfromCCurKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrfromCCurClick(null);
		}
	}
	
	@UiHandler("txttoCCurID")
	void onTxttoCCurKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == 121){ // F10
			onBtnBwrtoCCurClick(null);
		}
	}
}
